package com.zpf.web.servlet;


import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.zpf.pojo.Address;
import com.zpf.pojo.Goods;
import com.zpf.pojo.GoodsType;
import com.zpf.pojo.User;
import com.zpf.service.AdminService;
import com.zpf.service.impl.AdminServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/adminservlet")
public class AdminServlet extends BaseServlet {
    AdminService adminService = new AdminServiceImpl();






    public void deleteUser (HttpServletRequest request, HttpServletResponse response) throws Exception {


    }
























        public String addGoodsType(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String goodsParent = request.getParameter("goodsParent");
        String typename = request.getParameter("typename");
        if (StringUtils.isEmpty(typename)) {
            System.out.println("数据有误，重新添加");
            return "redirect:/adminservlet?method=adminAdd";
        }
        Address address= new Address();
        address.setName(typename);
        address.setLevel(1);

        adminService.insert(address);


        return "redirect:/adminservlet?method=adminAdd";

    }

        public String adminAdd(HttpServletRequest request, HttpServletResponse response) throws Exception {

        List<GoodsType> goodsTypes = adminService.AdminAdd();

        request.setAttribute("goodsTypeList",goodsTypes);
        return"forward:/admin/addGoodsType.jsp";


    }




        //商品管理
    public String getGoodsList(HttpServletRequest request, HttpServletResponse response) throws Exception {



        List<Goods> goods = adminService.getAdminGoodsList();

        request.setAttribute("goodsList",goods);
        return"forward:/admin/showGoods.jsp";

    }



    //商品管理
    public String adminShow(HttpServletRequest request, HttpServletResponse response) throws Exception {



        List<GoodsType> goodsTypes = adminService.AdminByShow();

    request.setAttribute("goodsTypeList",goodsTypes);
        return"forward:/admin/showGoodsType.jsp";

    }








        //会员管理详情(查看会员详情)
    public void getUserList(HttpServletRequest request, HttpServletResponse response) throws Exception {


        //根据用户role=1查询管理系统
        List<User> users = adminService.findAdminByRole();


        Object json = JSON.toJSON(users);

        response.setCharacterEncoding("utf-8");

        response.getWriter().print(json);

    }


        //退出

    public String adminLogOut(HttpServletRequest request, HttpServletResponse response) throws Exception {
    //清除session
        request.getSession().removeAttribute("user");
        request.getSession().invalidate();

        //清除cookie
        Cookie cookie = new Cookie("userinfo-2202","");
        //生存时间
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);

        return "redirect:/admin/login.jsp";
    }







    public String adminLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String username = request.getParameter("username");

        String password = request.getParameter("password");

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return "redirect:/admin/login.jsp";
        }
        User user = adminService.adminlogin(username,password);
        if (user != null) {
            request.getSession().setAttribute("user",user);

            return "redirect:/admin/admin.jsp";
        }else {
            return "redirect:/admin/login.jsp";
        }


    }
}